! function(window, document, $, undefined) {
    let init = function() {
        renderList()
        initEvent()
    }
    let initEvent = function() {
        $("#saveBtn").on("click", addUserClik);
        $(".test").on("click", ".delBtn", delUserClick)
        $(".test").on("click", ".updateBtn", updateUserClick)
    }

    let updateUserClick = function() {
        $("#username").val($(this).parent().siblings(".username").html())
        $("#age").val($(this).parent().siblings(".age").html())
        $("#register").val($(this).parent().siblings(".register").html())
        $("#email").val($(this).parent().siblings(".email").html())
        $("#telephone").val($(this).parent().siblings(".telephone").html())
        $("#enData").val($(this).parent().siblings(".enData").html())

        $("#saveBtn").html("修改")
        $(".modal-title").html("修改学生信息")

        window.id = $(this).attr("uid");

    }


    let delUserClick = function() {
        let id = $(this).attr("uid");
        $.ajax({
            type: "get",
            url: "/user/delUser",
            data: {
                id
            },
            dataType: "json",
            success: function(response) {
                renderList()
            }
        });
    }

    let addUserClik = function() {

        let username = $("#username").val();
        let age = $("#age").val();
        let register = $("#register").val();
        let telephone = $("#telephone").val();
        let email = $("#email").val();
        let enData = $("#enData").val();

        let url = "";
        let data = null;


        if ($(this).html() == "修改") {
            url = "/user/updateUser";
            data = {
                username,
                age,
                register,
                telephone,
                email,
                enData,
                id: window.id
            }
        } else {
            url = "/user/addUser"
            data = {
                username,
                age,
                register,
                telephone,
                email,
                enData
            }
        }

        $.ajax({
            type: "get",
            url,
            data,
            dataType: "json",
            success: function(response) {
                console.log(response)
                renderList()
            }
        });

    }

    let renderList = function() {
        let arr = [];
        $.ajax({
            type: "get",
            url: "/user/findUser",
            dataType: "json",
            success: function(response) {
                $.each(response, function(index, item) {
                    arr.push(`<tr>
                       <td scope="col"><input type="checkbox" class="allCheck"></td>
            <td scope="col" class="username">${item.username}</td>
            <td scope="col" class="age">${item.age}</td>
            <td scope="col" class="register">${item.register}</td>
            <td scope="col" class="telephone">${item.telephone}</td>
            <td scope="col" class="email">${item.email}</td>
            <td scope="col" class="enData">${item.enData}</td>
            <td scope="col"><button uid="${item._id}" class="delBtn">删除</button><button  uid="${item._id}" class="updateBtn" data-toggle="modal" data-target="#myModal">修改</button></td>
                    </tr>`)
                })

                $(".test").html(arr.join(""))
            }
        });
    }


    init()
}(window, document, jQuery)